package com.longteng.diamond.service;

import com.alibaba.fastjson.JSON;
import com.google.common.base.Function;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.longteng.diamond.constant.BuildTestStatus;
import com.longteng.diamond.dao.*;
import com.longteng.diamond.domain.*;
import com.longteng.diamond.po.RequestResult;
import com.longteng.diamond.po.TablePage;
import lombok.extern.slf4j.Slf4j;
import org.assertj.core.util.Sets;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.*;

/**
 * Created by Administrator
 * DATE : 2018/8/11
 * TIME : 23:13
 * PROJECT : java-lesson
 * PACKAGE : com.longteng.lesson
 *
 * @author <a href="mailto:taki@qq.com">Administrator</a>
 */
@Component
@Slf4j
public class BuildTestService {
    @Autowired
    BuildTestMapper buildTestMapper;
    @Autowired
    BuildTestInterfaceMapper buildTestInterfaceMapper;
    @Autowired
    BuildTestCaseMapper buildTestCaseMapper;
    @Autowired
    InterfaceTestMapper interfaceTestMapper;
    @Autowired
    InterfaceTestService interfaceTestService;
    @Autowired
    TestPlanService testPlanService;
    @Autowired
    TestCaseMapper testCaseMapper;

    /**
     * 构建临时测试计划,不保存在计划中,保存在构建列表
     *
     * @return list
     */
    @Transactional(rollbackFor = Exception.class)
    public Integer saveBuildTest(BuildTest buildTest, Integer[] interfaceIds) {
        /**插入一个构建信息,状态、执行用户**/
        buildTest.setTestPlanName("临时运行的计划");
        buildTestMapper.addBuildTest(buildTest);
        Integer buildTestId = buildTest.getBuildTestId();
        /**通过接口的ids获取接口的信息**/
        List<InterfaceTest> interfaceTestList = interfaceTestService.findInterfaceTestByIds(Arrays.asList(interfaceIds));
        /**所有接口信息新增到构建的接口表**/
        this.addBuildInterface(interfaceTestList, buildTestId);
        /**增加构建用例**/
        List<TestCase> testCaseList = testCaseMapper.findTestCaseInInterfaceId(Arrays.asList(interfaceIds));
        if (!CollectionUtils.isEmpty(testCaseList)) {
            this.addTestCase(testCaseList, buildTestId);
        }
        return buildTestId;
    }

    /**
     * 构建临时测试计划,不保存在计划中,保存在构建列表
     *
     * @return list
     */
    @Transactional(rollbackFor = Exception.class)
    public Integer buildTestPlan(BuildTest buildTest) {
        TestPlan testPlan = testPlanService.getTestPlanById(buildTest.getTestPlanId());
        List<TestPlanDetail> testPlanDetailList =testPlan.getTestPlanDetailList();
        List<Integer> testCaseIds =Lists.transform(testPlanDetailList, new Function<TestPlanDetail, Integer>() {
            @Override
            public Integer apply(TestPlanDetail testPlanDetail){
                return testPlanDetail.getTestCaseId();
            }
        });
        /**插入一个构建信息,状态、执行用户**/
        buildTestMapper.addBuildTest(buildTest);
        Integer buildTestId = buildTest.getBuildTestId();
        List<TestCase> testCaseList = testCaseMapper.findTestCaseInId(testCaseIds);
        Set<Integer> interfaceIds = new HashSet<>();
        for(TestCase testCase:testCaseList){
            interfaceIds.add(testCase.getInterfaceId());
        }
        List interfaceIdList =new ArrayList<Integer>(interfaceIds);
        List<InterfaceTest> interfaceTestList= interfaceTestService.findInterfaceTestByIds(interfaceIdList);
        /**所有接口信息新增到构建的接口表**/
        this.addBuildInterface(interfaceTestList, buildTestId);
        this.addTestCase(testCaseList, buildTestId);
        return buildTestId;
    }

    /**
     * 新增构建接口
     *
     * @param buildTestInterfaceList
     * @param buildTestId
     * @return
     */
    private void addBuildInterface(List<InterfaceTest> buildTestInterfaceList, Integer buildTestId) {
        Map buildInterfaceMap = Maps.newHashMap();
        buildInterfaceMap.put("buildTestInterfaceList", buildTestInterfaceList);
        buildInterfaceMap.put("buildTestId", buildTestId);
        /*增加构建接口*/
        buildTestInterfaceMapper.addBuildTestInterfaces(buildInterfaceMap);
    }

    /**
     * 新增构建测试用例
     *
     * @param buildTestCaseList
     * @param buildTestId
     * @return
     */
    private void addTestCase(List<TestCase> buildTestCaseList, Integer buildTestId) {
        Map map = Maps.newHashMap();
        map.put("buildTestCaseList", buildTestCaseList);
        map.put("buildTestId", buildTestId);
        buildTestCaseMapper.addBuildTestCases(map);
    }

    /**
     * 构建测试计划
     *
     * @param caseIds
     * @param buildTest
     * @return RequestResult
     */
    @Transactional(rollbackFor = Exception.class)
    public Integer saveBuildTestCase(BuildTest buildTest, Integer[] caseIds) {
        buildTest.setTestPlanName("临时运行的计划");
        buildTestMapper.addBuildTest(buildTest);
        Integer buildTestId = buildTest.getBuildTestId();
        InterfaceTest interfaceTest = interfaceTestService.findInterfaceTestById(buildTest.getInterfaceId());
        BuildTestInterface buildTestInterface = new BuildTestInterface();
        BeanUtils.copyProperties(interfaceTest, buildTestInterface);
        List list = Lists.newArrayList();
        list.add(buildTestInterface);
        this.addBuildInterface(list, buildTestId);
        List<TestCase> caseList = testCaseMapper.findTestCaseInId(Arrays.asList(caseIds));
        this.addTestCase(caseList, buildTestId);
        return buildTestId;
    }

    /**
     * 根据项目获取运行中的构建
     *
     * @param projectId
     * @return BuildTest
     */
    public BuildTest getBuildTestRunning(Integer projectId) {
        BuildTest buildTest = new BuildTest();
        buildTest.setProjectId(projectId);
        buildTest.setStatus(BuildTestStatus.running);
        List<BuildTest> buildTestList = buildTestMapper.getBuildTestReturnMap(buildTest);
        if (!CollectionUtils.isEmpty(buildTestList)) {
            return buildTestList.get(0);
        }
        return null;
    }

    /**
     * 获取构建信息
     *
     * @param projectId
     * @return
     */
    public List<BuildTest> getBuildTestByProjectId(Integer projectId) {
        BuildTest buildTest = new BuildTest();
        buildTest.setPageNo(0);
        buildTest.setPageSize(25);
        buildTest.setProjectId(projectId);
        List<BuildTest> buildTestList = buildTestMapper.getBuildTest(buildTest);
        return buildTestList;
    }

    /**
     * 构建测试
     *
     * @param buildTestId
     * @return
     */
    public BuildTest buildRunTest(Integer buildTestId) {
        BuildTest buildTest = buildTestMapper.findBuildTestById(buildTestId);
        return buildTest;
    }

    /**
     * 更新构建接口状态
     *
     * @param buildTestInterface
     * @return
     */
    public void updateBuildInterface(BuildTestInterface buildTestInterface) {
        buildTestInterfaceMapper.updateBuildInterface(buildTestInterface);
    }

    /**
     * 更新构建测试用例状态
     *
     * @param testCaseList
     * @return
     */
    public void updateBuildTestCases(List<BuildTestCase> testCaseList) {
        buildTestCaseMapper.updateBuildTestCases(testCaseList);
    }

    /**
     * 更新构建状态
     *
     * @param buildTest
     * @return
     */
    public void updateBuildTest(BuildTest buildTest) {
        buildTestMapper.updateBuildTest(buildTest);
    }


    /**
     * @param testCase
     * @return interfaceTest
     * @description 获取用例
     */
    public TablePage getTestCase(BuildTestCase testCase) {
        TablePage tablePage = new TablePage();
        try {
            List<BuildTestCase> buildTestCaseList = buildTestCaseMapper.findBuildTestCaseTable(testCase);
            tablePage.setRows(buildTestCaseList);
            long total = buildTestCaseMapper.queryAllRow(testCase);
            tablePage.setTotal(total);
        } catch (Exception e) {
            log.error("查询接口列表异常", e);
        }
        return tablePage;
    }
}
